{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1=1\t\n",
      "1*2=2\t2*2=4\t\n",
      "1*3=3\t2*3=6\t3*3=9\t\n",
      "1*4=4\t2*4=8\t3*4=12\t4*4=16\t\n",
      "1*5=5\t2*5=10\t3*5=15\t4*5=20\t5*5=25\t\n",
      "1*6=6\t2*6=12\t3*6=18\t4*6=24\t5*6=30\t6*6=36\t\n",
      "1*7=7\t2*7=14\t3*7=21\t4*7=28\t5*7=35\t6*7=42\t7*7=49\t\n",
      "1*8=8\t2*8=16\t3*8=24\t4*8=32\t5*8=40\t6*8=48\t7*8=56\t8*8=64\t\n",
      "1*9=9\t2*9=18\t3*9=27\t4*9=36\t5*9=45\t6*9=54\t7*9=63\t8*9=72\t9*9=81\t\n"
     ]
    }
   ],
   "source": [
    "for i in range(1, 10):\n",
    "    for j in range(1, i + 1):\n",
    "        print(f\"{j}*{i}={i * j}\", end=\"\\t\")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "45 = 3 * 3 * 5"
     ]
    }
   ],
   "source": [
    "num = 45\n",
    "print(num, \"=\", end=\" \")\n",
    "factor = 2\n",
    "while num > 1:\n",
    "    while num % factor == 0:\n",
    "        print(factor, end=\"\")\n",
    "        num //= factor\n",
    "        if num > 1:\n",
    "            print(\" * \", end=\"\")\n",
    "    factor += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这个数是4位数\n",
      "逆序打印各位数字:\n",
      "311\n"
     ]
    }
   ],
   "source": [
    "num = int(input(\"请输入一个正整数:1130 \"))  # 获取用户输入的正整数，转换为整数类型\n",
    "count = 0  # 用于记录数字的位数\n",
    "temp_num = num  # 用一个临时变量来保存原始输入数字，方便后续操作，避免改变原始的num值\n",
    "while temp_num > 0:  # 通过循环，每次除以10去掉最低位数字，来统计位数\n",
    "    count += 1\n",
    "    temp_num //= 10\n",
    "\n",
    "print(\"这个数是{}位数\".format(count))\n",
    "\n",
    "reverse_num = 0  # 用于存储逆序后的数字\n",
    "while num > 0:  # 通过循环取原数字的最低位数字，构建逆序数字\n",
    "    digit = num % 10  # 获取当前数字的最低位数字\n",
    "    reverse_num = reverse_num * 10 + digit  # 将最低位数字添加到逆序数字中\n",
    "    num //= 10  # 去掉已经处理过的最低位数字\n",
    "\n",
    "print(\"逆序打印各位数字:\")\n",
    "print(reverse_num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "61725\n"
     ]
    }
   ],
   "source": [
    "a = 5\n",
    "n = 5  # 这里假设项数为5，你可以根据实际需求修改\n",
    "s = 0\n",
    "for i in range(1, n + 1):\n",
    "    num_str = str(a) * i  # 通过字符串重复操作得到每一项对应的字符串形式，比如 \"5\", \"55\", \"555\"等\n",
    "    num = int(num_str)  # 将字符串形式转换为整数\n",
    "    s += num  # 累加每一项的值\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "13.24374595992243\n"
     ]
    }
   ],
   "source": [
    "N = int(input(\"请输入项数N:8 \"))  # 获取用户输入的项数N，并转换为整数类型\n",
    "sum_value = 0  # 用于存储前N项之和，初始化为0\n",
    "a, b = 2, 1  # 初始化分数序列的第一项分子为2，分母为1\n",
    "\n",
    "for _ in range(N):  # 循环N次，依次计算每一项并累加\n",
    "    sum_value += a / b  # 将当前项的值累加到总和中\n",
    "    a, b = a + b, a  # 更新下一项的分子和分母，按照规律，下一项分子是前一项分子分母之和，分母是前一项分子\n",
    "print(sum_value)  # 输出前N项之和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4256\n"
     ]
    }
   ],
   "source": [
    "for num in range(-100, 1000000):  # 可根据实际情况适当扩大范围，这里先大致设定一个搜索范围\n",
    "    x = int((num + 100) ** 0.5)  # 计算加上100后开平方的结果并取整\n",
    "    y = int((num + 100 + 268) ** 0.5)  # 计算加上100又加上268后开平方的结果并取整\n",
    "    if x ** 2 == num + 100 and y ** 2 == num + 100 + 268:  # 判断是否满足完全平方数的条件\n",
    "        print(num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第10个月兔子总数为55对\n"
     ]
    }
   ],
   "source": [
    "def rabbit_count(n):\n",
    "    if n == 1 or n == 2:\n",
    "        return 1\n",
    "    return rabbit_count(n - 1) + rabbit_count(n - 2)\n",
    "\n",
    "n = int(input(\"请输入要查看的月数: \"))  # 获取用户输入的月数，并转换为整数类型\n",
    "result = rabbit_count(n)\n",
    "print(\"第{}个月兔子总数为{}对\".format(n, result))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该矩阵对角线元素之和为: 155\n"
     ]
    }
   ],
   "source": [
    "# 创建一个空的3x3矩阵\n",
    "matrix = []\n",
    "# 通过循环获取用户输入，填充矩阵元素\n",
    "for i in range(3):\n",
    "    row = input(f\"请输入矩阵第{i + 1}行的3个元素，以空格隔开: \").split()\n",
    "    # 将输入的字符串元素转换为整数类型\n",
    "    row = [int(num) for num in row]\n",
    "    matrix.append(row)\n",
    "\n",
    "# 初始化对角线元素之和为0\n",
    "sum_diagonal = 0\n",
    "# 计算主对角线元素之和（左上角到右下角）\n",
    "for i in range(3):\n",
    "    sum_diagonal += matrix[i][i]\n",
    "# 计算副对角线元素之和（右上角到左下角）\n",
    "for i in range(3):\n",
    "    sum_diagonal += matrix[i][2 - i]\n",
    "\n",
    "# 由于主副对角线中心元素重复相加了一次，需要减去重复的部分（如果矩阵是奇数阶的话）\n",
    "if len(matrix) % 2 == 1:\n",
    "    sum_diagonal -= matrix[len(matrix) // 2][len(matrix) // 2]\n",
    "\n",
    "print(\"该矩阵对角线元素之和为:\", sum_diagonal)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6, 7, 8]\n"
     ]
    }
   ],
   "source": [
    "n = int(input(\"请输入整数的个数n: \"))\n",
    "m = int(input(\"请输入移动的位置数m: \"))\n",
    "nums = []\n",
    "for _ in range(n):\n",
    "    num = int(input(\"请输入一个整数: \"))\n",
    "    nums.append(num)\n",
    "\n",
    "m %= n  # 防止m大于n，取m除以n的余数，确保移动位置在合理范围内\n",
    "\n",
    "nums = nums[-m:] + nums[:-m]  # 通过切片操作进行位置移动\n",
    "print(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "结果是: 1.5333333333333332\n"
     ]
    }
   ],
   "source": [
    "def sum_fractions(n):\n",
    "    result = 0\n",
    "    if n % 2 == 0:\n",
    "        for i in range(2, n + 1, 2):\n",
    "            result += 1 / i\n",
    "    else:\n",
    "        for i in range(1, n + 1, 2):\n",
    "            result += 1 / i\n",
    "    return result\n",
    "n = int(input(\"请输入一个整数: \"))\n",
    "sum_result = sum_fractions(n)\n",
    "print(\"结果是:\", sum_result)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
